﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Sitemap : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["Resist"].ConnectionString;

	protected void Page_Load(object sender, EventArgs e)
	{
		Response.Clear();
		Response.AddHeader("Content-Type", "text/xml");
		Response.Write(BuildXml());
		Response.End();
	}

	private string BuildXml()
	{
		string result = "<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";

		DateTime lastPost = (DateTime)KC.DataAccess.Global.Global.CallSql_Query(cs, "select max([created]) from posts").Rows[0][0];

		// Home
		result += "<url>\r\n" +
			"<loc>http://www.ResistOccupation.com</loc>\r\n" +
			"<lastmod>" + lastPost.ToString("yyyy-MM-dd") + "</lastmod>\r\n" +
			"<changefreq>hourly</changefreq>\r\n" +
			"<priority>1.0</priority>\r\n" +
			"</url>\r\n";
		result += "<url>\r\n" +
			"<loc>http://www.ResistOccupation.com/archives.aspx</loc>\r\n" +
			"<lastmod>" + lastPost.ToString("yyyy-MM-dd") + "</lastmod>\r\n" +
			"<changefreq>daily</changefreq>\r\n" +
			"<priority>0.3</priority>\r\n" +
			"</url>\r\n";
		result += "<url>\r\n" +
			"<loc>http://www.ResistOccupation.com/Categories.aspx</loc>\r\n" +
			"<lastmod>" + lastPost.ToString("yyyy-MM-dd") + "</lastmod>\r\n" +
			"<changefreq>daily</changefreq>\r\n" +
			"<priority>0.3</priority>\r\n" +
			"</url>\r\n";

		// posts
		DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, "select FriendlyName, [created] from Posts order by id desc");
		for (int i = 0; i < dt.Rows.Count; i++)
			result += "<url>\r\n" +
				"<loc>http://www.ResistOccupation.com/post.aspx?" + (string)dt.Rows[i]["FriendlyName"] + "</loc>\r\n" +
				"<lastmod>" + ((DateTime)dt.Rows[i]["created"]).ToString("yyyy-MM-dd") + "</lastmod>\r\n" +
				"<changefreq>" + (i < 2 ? "hourly" : i < 10 ? "daily" : "weekly") + "</changefreq>\r\n" +
				"<priority>" + (i < 2 ? "1.0" : i < 10 ? "0.8" : "0.3") + "</priority>\r\n" +
				"</url>\r\n";

		// Categories
		DataTable dtCat = KC.DataAccess.Global.Global.CallSql_Query(cs, "select FriendlyName from Categories order by id desc");
		for (int i = 0; i < dtCat.Rows.Count; i++)
			result += "<url>\r\n" +
				"<loc>http://www.ResistOccupation.com/Categories.aspx?" + (string)dtCat.Rows[i]["FriendlyName"] + "</loc>\r\n" +
				"<lastmod>" + ((DateTime)dt.Rows[0]["created"]).ToString("yyyy-MM-dd") + "</lastmod>\r\n" +
				"<changefreq>daily</changefreq>\r\n" +
				"<priority>0.3</priority>\r\n" +
				"</url>\r\n";

		result += "</urlset>\r\n";
		return result;
	}
}